package com.zzyl.nursing.service.impl;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {

    public static final String OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    public static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
    public static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;


    @Override
    public String getOpenid(String code) {
        Map<String , Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        paramMap.put("js_code", code);
        paramMap.put("grant_type", "authorization_code");
        String result = HttpUtil.get(OPEN_ID_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //检查是否有错误码,如果没有错误码或者错误码为0,则返回openid
        if(jsonObject.getInt("errcode") == null || jsonObject.getInt("errcode") == 0){
            return jsonObject.getStr("openid","未知错误");
        }else {
            throw new RuntimeException("获取openid失败,错误码:" + jsonObject.toJSONString(1));
        }
    }

    @Override
    public String getToken() {
        Map<String , Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", secret);
        paramMap.put("grant_type", "client_credential");
        String result = HttpUtil.get(TOKEN_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        //检查是否有错误码,如果没有错误码或者错误码为0,则返回token
        if(jsonObject.getInt("errcode") == null || jsonObject.getInt("errcode") == 0){
            return jsonObject.getStr("access_token");
        }else {
            throw new RuntimeException("获取token失败,错误码:" + jsonObject.toJSONString(1));
        }
    }

    @Override
    public String getPhone(String detailCode) {

            String token = getToken();
            // 构造请求参数
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("code", detailCode);
            // 使用HttpUtil发送POST请求
            String result = HttpUtil.post(PHONE_URL + token,JSONUtil.toJsonStr(paramMap));
            // 解析响应结果
            JSONObject jsonObject = JSONUtil.parseObj(result);
            //判断调用成功还是失败
            if(jsonObject.getInt("errcode") == null || jsonObject.getInt("errcode") == 0){
                //成功
                JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
                return phoneInfo.getStr("phoneNumber");
            }else {
                throw new RuntimeException("调用微信获取手机号接口失败:" + jsonObject.toJSONString(1));
            }
    }

}
